package org.anderground.editor.gui.properties.station;

import java.util.Arrays;
import java.util.List;

import org.anderground.core.model.Station;
import org.anderground.core.model.Tunnel;
import org.anderground.editor.gui.properties.AbstractPropertiesPanel;

@SuppressWarnings("serial")
public class StationsPropertiesPanel extends AbstractPropertiesPanel<Station> {

	private Station[] stations;
	private boolean sort = true;

	public StationsPropertiesPanel() {
		super(new StationNameProperty(), new StationPositionProperty(),
				new StationLocationProperty(), new StationScheduleProperty());
	}

	public void setSelection(Station[] stations) {

		if (sort) {
			this.stations = Arrays.copyOf(stations, stations.length);
			Arrays.sort(this.stations);

		} else
			this.stations = stations;

		this.updateView();
	}

	@Override
	public void onSelectionChanged(List<Station> selectedStations,
			List<Tunnel> selectedTunnel) {

		if (selectedStations != null && !selectedStations.isEmpty()) {
			setSelection(selectedStations.toArray(new Station[0]));
		}

	}

	@Override
	protected int getRowCount() {
		return (stations == null) ? 0 : stations.length;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		return (stations == null || rowIndex < 0 || rowIndex >= stations.length) ? null
				: stations[rowIndex];
	}

}